package cn.plushine.controller;

import cn.plushine.pojo.Brand;
import cn.plushine.service.BaseBrandService;
import cn.plushine.utils.ResMapUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * 品牌信息
 */
@Api(tags = "品牌信息模块")
@RestController
@RequestMapping("/admin/product/brand/")
public class BaseBrandController {

    @Autowired
    BaseBrandService baseBrandService;


    // 获取某一页的品牌信息
    @ApiOperation(value = "获取某一页的品牌信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页码", example = "1", required = true, paramType = "path", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页记录数", example = "1", required = true, paramType = "path", dataType = "int"),
            @ApiImplicitParam(name = "input", value = "筛选条件", paramType = "query", dataType = "String")
    })
    @GetMapping("/{current}/{size}")
    public HashMap<String, Object> getPaginationBrandList(@PathVariable int current, @PathVariable int size, String input) {
        try {
            // 计算起始索引
            int index = size * (current - 1);
            // 获取品牌信息列表
            List<Brand> brandListDB = baseBrandService.getBrandList(index, size, input);

            // 获取数据总数
            int total = baseBrandService.getBrandTotal(input);

            // 第一次封装要返回的数据
            HashMap<String, Object> map = new HashMap<>();
            map.put("records", brandListDB);
            map.put("total", total);

            // 信息获取成功
            return ResMapUtils.encapsulation(200, "获取品牌信息列表成功！", map);
        } catch (Exception e) {
            return ResMapUtils.encapsulation(201, "获取品牌信息列表失败！", null);
        }
    }

    // 添加品牌信息
    @ApiOperation(value = "添加品牌信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "brand", value = "品牌信息", required = true, paramType = "body", dataType = "Brand")
    })
    @PostMapping("save")
    public HashMap<String, Object> save(@RequestBody Brand brand) {
        try {
            // 添加品牌信息
            baseBrandService.save(brand);
            // 成功响应
            return ResMapUtils.encapsulation(200, "添加品牌信息成功！", null);
        } catch (Exception e) {
            // 添加失败
            return ResMapUtils.encapsulation(201, "添加品牌信息失败！", null);
        }
    }

    // 更新品牌信息
    @ApiOperation(value = "更新品牌信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "brand", value = "品牌信息", required = true, paramType = "body", dataType = "Brand")
    })
    @PostMapping("update")
    public HashMap<String, Object> update(@RequestBody Brand brand) {
        try {
            System.out.println(brand.toString());
            // 更新品牌信息
            baseBrandService.update(brand);
            // 成功响应
            return ResMapUtils.encapsulation(200, "品牌信息更新成功！", null);
        } catch (Exception e) {
            // 添加失败
            return ResMapUtils.encapsulation(201, "品牌信息更新失败！", null);
        }
    }

    // 删除品牌信息
    @ApiOperation(value = "删除品牌信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "品牌id", example = "1", required = true, paramType = "path", dataType = "int")
    })
    @DeleteMapping("delete/{id}")
    public HashMap<String, Object> delete(@PathVariable int id) {
        try {
            // 删除品牌信息
            baseBrandService.delete(id);
            // 成功响应
            return ResMapUtils.encapsulation(200, "删除品牌信息成功！", null);
        } catch (Exception e) {
            // 删除失败
            return ResMapUtils.encapsulation(201, "删除品牌信息失败！", null);
        }
    }
}
